using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.UIElements;

public class Cenario : MonoBehaviour {

    public GameObject armadilhaPrefab, bolhasPrefab;
    public Player player;
    public List<GameObject> armadilhaGO, bolhasGO, portais;
    private int range = 100;

    // Inicialzia antes de começar o jogo
    void Start() {

        player = FindAnyObjectByType<Player>(); // Procura o Jogador

        Scene startScene = SceneManager.GetSceneByName("Start"); // Pega o cenario Inicial
        Scene aguaScene = SceneManager.GetSceneByName("Agua"); // Pega o cenario Agua
        Scene arScene = SceneManager.GetSceneByName("Ar"); // Pega o cenario Ar
        Scene terraScene = SceneManager.GetSceneByName("Terra"); // Pega o cenario Terra
        Scene fogoScene = SceneManager.GetSceneByName("Fogo"); // Pega o cenario Fogo
        Scene vazioScene = SceneManager.GetSceneByName("Vazio"); // Pega o cenario Vazio

        // Define cada cenario e seus obstaculos
        for (int i = 0; i < 20 + player.pontuacao/10; i++) {

            if (SceneManager.GetActiveScene() == aguaScene || SceneManager.GetActiveScene() == startScene)
            {
                armadilhaPrefab = Resources.Load<GameObject>("Objects/Coral");
            }
            else if (SceneManager.GetActiveScene() == terraScene)
            {
                armadilhaPrefab = Resources.Load<GameObject>("Objects/Rochas");
            }
            else if (SceneManager.GetActiveScene() == vazioScene)
            {
                armadilhaPrefab = Resources.Load<GameObject>("Objects/Planeta");
            }
            else if (SceneManager.GetActiveScene() == fogoScene)
            {
                armadilhaPrefab = Resources.Load<GameObject>("Objects/Fogueira");
            }
            else if (SceneManager.GetActiveScene() == arScene)
            {
                armadilhaPrefab = Resources.Load<GameObject>("Objects/Aviao");
            }

            bolhasPrefab = Resources.Load<GameObject>("Objects/BolhaAzul");
        }

        Instanciar();
    }

    private void Instanciar() {
        for (int i = 0; i < 20 + player.pontuacao / 10; i++) {
            float iAx = UnityEngine.Random.Range(-range, range);
            float iAz = UnityEngine.Random.Range(-range, range);
            if (iAx < -5 && iAx > 5) {
                iAx = UnityEngine.Random.Range(-range, range);
            }
            if (iAz < -5 && iAz > 5) {
                iAz = UnityEngine.Random.Range(-range, range);
            }
            armadilhaGO.Add(Instantiate(armadilhaPrefab, new Vector3(iAx, armadilhaPrefab.transform.position.y + 1.3f, iAz), Quaternion.identity));
            armadilhaGO[i].name = "Armadilha";
        }

        for (int i = 0; i < 20 + player.pontuacao / 10; i++) {
            float iBx = UnityEngine.Random.Range(-range, range);
            float iBz = UnityEngine.Random.Range(-range, range);
            bolhasGO.Add(Instantiate(bolhasPrefab, new Vector3(iBx, armadilhaPrefab.transform.position.y, iBz), Quaternion.identity));
            bolhasGO[i].name = "Bolha";
        }
    }
}